package com.example.tablefield.config;

import com.alibaba.fastjson.JSON;
import com.example.tablefield.entites.UserWork;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.MappedTypes;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

@MappedJdbcTypes(JdbcType.VARCHAR)
@MappedTypes(UserWork.class)
public class UserWorkTypeHandler extends BaseTypeHandler<UserWork> {
    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, UserWork parameter, JdbcType jdbcType) throws SQLException {
        ps.setString(i, JSON.toJSONString(parameter));
        System.out.println();
    }

    @Override
    public UserWork getNullableResult(ResultSet rs, String columnName) throws SQLException {
        String jsonString = rs.getString(columnName);
        return JSON.parseObject(jsonString, UserWork.class);
    }

    @Override
    public UserWork getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
        return null;
    }

    @Override
    public UserWork getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
        return null;
    }
}
